package com.lfs.feishu.controller;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonParser;
import com.lark.oapi.Client;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.DeleteBatchMessageReq;
import com.lark.oapi.service.im.v1.model.DeleteBatchMessageResp;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

import static com.lfs.feishu.constant.AppInfo.APP_ID;
import static com.lfs.feishu.constant.AppInfo.APP_SECRET;

@AllArgsConstructor
@RestController
@RequestMapping("/feishu/batch")
@Slf4j
public class FeishuBatchWithdrawController {



    @GetMapping("/withdraw")
    public void withdraw(@RequestParam String msgId) throws Exception {
        // 构建client
        Client client = Client.newBuilder(APP_ID, APP_SECRET).build();

        // 创建请求对象
        DeleteBatchMessageReq req = DeleteBatchMessageReq.newBuilder()
                .batchMessageId(msgId)
                .build();

        // 发起请求
        DeleteBatchMessageResp resp = client.im().v1().batchMessage().delete(req);

        // 处理服务端错误
        if (!resp.success()) {
            log.error("删除消息失败: {}", Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8))));
            throw new RuntimeException("批量删除消息失败");
        }

        log.info("删除结果：{}",JSON.toJSONString(resp));
    }
}
